package day_2022_1_to_2.day16;

public class Day13_2_2 {
    public static void main(String[] args) {
    String s="a-bC-dEf-ghIj";
    System.out.println(reverseOnlyLetters(s));
    }

    public static String reverseOnlyLetters(String s) {
        //将字符串转为字符数组
        char[] data=s.toCharArray();

        //new 一个arr数组存非字母，得到如 [a,a,-,a,-,a]形式的数组
        char[] arr=new char[data.length];
        for (int i = 0; i < data.length; i++) {
            int value=Integer.valueOf(data[i]);
            if(isNotLetter(value)){
                arr[i]=data[i];
            }else {
                arr[i]='a';
            }
        }

        //得到相应形式数组
        int k=0;
        for (int i = data.length-1; i >=0 ; ) {
            if(!(isNotLetter(data[i])) && !(isNotLetter(arr[k]))){
                arr[k]=data[i];
                i--;
                k++;
            }
            else if(!(isNotLetter(data[i])) && (isNotLetter(arr[k]))){
                k++;
            }
            else if((isNotLetter(data[i])) && !(isNotLetter(arr[k]))){
                i--;
            }
            else if((isNotLetter(data[i])) && (isNotLetter(arr[k]))){
                i--;
                k++;
            }
            //当k值走到末尾时跳出循环
            if(k==data.length){
                break;
            }
        }

        //数组转为字符串
        StringBuilder sb2=new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            sb2.append(arr[i]);
        }
        return sb2.toString();
    }

    //判断 不是字母，返回true.
    public static boolean isNotLetter(int value){
        if(value<65 || value>90&&value<97 || value>122 ){
            return true;
        }
        return false;
    }
}
